//
//  HomeViewController.m
//  PaiLeXiongProject
//
//  Created by 小獾科技 on 2017/8/15.
//  Copyright © 2017年 小獾科技. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeBannerCell.h"
#import "HomeCategoryCell.h"
#import "AdvertisingViewController.h"
#import "HomeActivityCell.h"
#import "HomeRecommendCell.h"
#import "HomeListTableViewCell.h"
#import <QuartzCore/QuartzCore.h>
#import "GW_TextField.h"
#import "HomeRecommendDetailViewController.h"
#import "SearchViewController.h"
#import "HomeBannerModel.h"
#import "HomeIconModel.h"
#import "HomeListModel.h"
#import "HomeCitySelectViewController.h"
#import "SubmitOrderViewController.h"
#import "GuideViewController.h"
@interface HomeViewController ()<UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate>
@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) UIView *aView; //这个view的作用是使搜索的图片的占位符 调整位置
@property (nonatomic, strong) UIImageView *search; // 导航条 textfield内的搜索图标
@property (nonatomic, strong) UIButton *cityBtn;  // 导航条左边城市按钮
@property (nonatomic, strong) NSMutableArray *bannerArray;
@property (nonatomic, strong) NSMutableArray *activeArray;
@property (nonatomic, strong) NSMutableArray *iconArray;
@property (nonatomic, strong) NSMutableArray *datasource;
@property (nonatomic, strong) NSMutableArray *actionArray;

@property (nonatomic, strong) UIView *alert;
@property (nonatomic, strong) UIView *aview;
@property (nonatomic, strong) UILabel *cityLabel;
@property (nonatomic, strong) UILabel *topLabel;//导航条上的label

@property (nonatomic, copy) NSString *cityName;

@property (nonatomic, strong) LoadingView *load;
@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self setNavigationBarViewAndView];
//    self.edgesForExtendedLayout = UIRectEdgeTop;
    _load = [[LoadingView alloc] initWithFrame:CGRectMake(0, 0, GWIDTH, GHEIGHT)];
    _load.hidden = YES;
    if([GW_Request sharedInstance].noNetwork)
    {
        [_load setReminderLabelTitle:@"糟糕！断网了" andImage:@"noNetweok"];
    }
    else
    {
        [_load setReminderLabelTitle:@"暂无商品~" andImage:@"noNetweok"];
    }
    _load.refreshBtn.hidden = NO;
    [_load.refreshBtn addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_load];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleChn) name:@"homeChange" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertAction) name:@"alert" object:nil];
    
    
    //判断是否切换城市的接口
    NSDictionary *param = @{@"cityName":STRING(CITYNAME)};
    [NetWork sendPostRequestURL:[NSString stringWithFormat:@"%@/city/CityIsServe", GW_Domain] parameters:param completionBlockWithSuccess:^(NSDictionary *resultDict) {
        NSLog(@"定位 %@", resultDict);
        //        NSDictionary *dic = resultDict[@"data"];
        //        NSString *flag = [NSString stringWithFormat:@"%@", dic[@"flag"]];
        //        if([flag isEqualToString:@"1"])
        //        {
        //            [self createAlertView];
        //        }
    } andFailure:^(NSError *error, NSString *errorMsg) {
        if([errorMsg isEqualToString:@"指定的城市不在服务列表中"])
        {
            [self createAlertView];
        }
    }];
    
}

- (void)handleChn
{
    if([GW_Request sharedInstance].noNetwork)
    {
        _load.hidden = NO;
        [self.view bringSubviewToFront:_load];
    }
}

- (void)HomdedoRefresh
{
    [self setNetworking];
}

- (void)createAlertView
{
    _alert = [[UIView alloc] initWithFrame:CGRectMake(0, 0, GWIDTH, GHEIGHT)];
    _alert.hidden = YES;
    _alert.backgroundColor = ColorFrom16A(0x000000, 0.8);
    [[[UIApplication sharedApplication].delegate window] addSubview:_alert];    
    
    _aview = [UIView new];
    _aview.layer.backgroundColor = ColorWhite.CGColor;
    _aview.layer.cornerRadius = 12;
    [_alert addSubview:_aview];
    [_aview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_alert).offset(246);
        make.centerX.equalTo(_alert.mas_centerX);
        make.width.equalTo(@270);
        make.height.equalTo(@(98+40 + 10));
    }];
    
    UIView *circle = [UIView new];
    circle.layer.backgroundColor = ColorWhite.CGColor;
    circle.layer.cornerRadius = 4;
    circle.layer.borderColor = ColorFrom16(0xF5CB37).CGColor;
    circle.layer.borderWidth = 2;
    [_aview addSubview:circle];
    [circle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_aview).offset(26);
        make.left.equalTo(_aview).offset(113);
        make.width.equalTo(@8);
        make.height.equalTo(@8);
    }];
    
    _cityLabel = [UILabel new];
    _cityLabel.text = CITYNAME;
    _cityLabel.textColor = ColorFrom16(0xF5CB37);
    _cityLabel.font = PFRFONT(17);
    [_aview addSubview:_cityLabel];
    [_cityLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_aview).offset(18);
        make.left.equalTo(circle.mas_right).offset(3);
        make.right.equalTo(_aview).offset(-32);
        make.height.equalTo(@30);
    }];
    
    UILabel *detailLabel = [UILabel new];
    detailLabel.text = @"您目前定位城市暂未开设店铺，是否切换至其它城市进行探索？";
    detailLabel.textColor = ColorFrom16(0xA7A7A7);
    detailLabel.font = PFRFONT(13);
    detailLabel.numberOfLines = 2;
    detailLabel.textAlignment = 1;
    detailLabel.attributedText = [GW_Tool setTextColor:ColorFrom16(0xF5CB37) rangeOfString:@"未开设店铺" string:detailLabel.text textFont:13];
    [_aview addSubview:detailLabel];
    [detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_cityLabel.mas_bottom).offset(10);
        make.left.equalTo(_aview).offset(15);
        make.right.equalTo(_aview).offset(-15);
        make.height.greaterThanOrEqualTo(@32);
    }];
    
    UIView *line = [UIView new];
    line.backgroundColor = COLOR_BACKGROUND;
    [_aview addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(detailLabel.mas_bottom).offset(12);
        make.left.and.right.equalTo(_aview);
        make.height.equalTo(@1);
    }];
    
    UIButton *cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancleBtn setTitle:@"取消" forState:0];
    [cancleBtn setTitleColor:ColorFrom16(0xC2C2C2) forState:0];
    cancleBtn.titleLabel.font = PFRFONT(17);
    [cancleBtn addTarget:self action:@selector(handleCancleBtn) forControlEvents:UIControlEventTouchUpInside];
    [_aview addSubview:cancleBtn];
    [cancleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line.mas_bottom).offset(1);
        make.left.equalTo(_aview);
        make.width.equalTo(@134);
        make.height.equalTo(@40);
    }];
    
    UIView *line1 = [UIView new];
    line1.backgroundColor = COLOR_BACKGROUND;
    [_aview addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cancleBtn.mas_right).offset(1);
        make.top.equalTo(line.mas_bottom);
        make.bottom.equalTo(_aview);
        make.width.equalTo(@1);
    }];
    
    UIButton *changeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [changeBtn setTitle:@"切换" forState:0];
    [changeBtn setTitleColor:ColorFrom16(0xEDC330) forState:0];
    changeBtn.titleLabel.font = PFRFONT(17);
    [changeBtn addTarget:self action:@selector(handleChangeBtn) forControlEvents:UIControlEventTouchUpInside];
    [_aview addSubview:changeBtn];
    [changeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(cancleBtn);
        make.left.equalTo(line1.mas_right);
        make.width.equalTo(@134);
        make.height.equalTo(@40);
    }];
}

//取消按钮响应方法
- (void)handleCancleBtn
{
    _alert.hidden = YES;
    [self.alert removeFromSuperview];

}

//切换按钮响应方法
- (void)handleChangeBtn
{
    [self handleLocationAction];
    _alert.hidden = YES;
    [self.alert removeFromSuperview];

}

- (void)setNetworking
{
    _bannerArray = [NSMutableArray arrayWithCapacity:0];
    _iconArray = [NSMutableArray arrayWithCapacity:0];
    _activeArray = [NSMutableArray arrayWithCapacity:0];
    _datasource = [NSMutableArray arrayWithCapacity:0];
    _actionArray = [NSMutableArray arrayWithCapacity:0];
    [_bannerArray removeAllObjects];
    [_iconArray removeAllObjects];
    [_activeArray removeAllObjects];
    [_datasource removeAllObjects];
    [_actionArray removeAllObjects];
    @weakobj(self);
    [NetWork sendGetRequestURL:[NSString stringWithFormat:@"%@/index/index",GW_Domain] parameters:nil completionBlockWithSuccess:^(NSDictionary *resultDict) {
        NSLog(@"首页 %@", resultDict);
        @strongobj(self);
        NSDictionary *dict = resultDict[@"data"];
        for (NSDictionary *banner in dict[@"banner"]) {
            [self.bannerArray addObject:banner[@"imgurl"]];
            [self.actionArray addObject:banner[@"action"]];
        }
        for (NSDictionary *action in dict[@"active"]) {
            [_activeArray addObject:action[@"imgurl"]];
        }
        for (NSDictionary *icon in dict[@"icon"]) {
           HomeIconModel *model = [GW_Tool getObjFromDictionary:icon byClassName:@"HomeIconModel"];
            [_iconArray addObject:model];
        }
        for (NSDictionary *list in dict[@"baokuan"]) {
            HomeListModel *model = [GW_Tool getObjFromDictionary:list byClassName:@"HomeListModel"];
            [_datasource addObject:model];
        }
        [self.tableview.mj_header endRefreshing];
        [_tableview reloadData];
        dispatch_async(dispatch_get_main_queue(), ^{
            if(self.datasource.count > 0)
                self.load.hidden = YES;
            else
                self.load.hidden = NO;
        });
        
    } andFailure:^(NSError *error, NSString *errorMsg) {
        MESSAGE_TOAST(errorMsg);
        self.load.hidden = NO;
        [_tableview.mj_header endRefreshing];
        NSLog(@"首页错误%@", error);
    }];
}

- (void)handleRefresh:(UIButton *)sender
{
    [self HomdedoRefresh];
}

/***
 *  设置导航条以及UI
 */
- (void)setNavigationBarViewAndView
{
    [self addCityBtn];
    _topLabel = [[UILabel alloc] initWithFrame:CGRectMake(98, 0, GWIDTH - 98 - 44, 30)];
    _topLabel.text = @"      可输入店铺名、分类";
    _topLabel.textColor = ColorFrom16A(0x999999, 0.57);
    _topLabel.font = PFRFONT(12);
    _topLabel.layer.backgroundColor = ColorWhite.CGColor;
    _topLabel.layer.cornerRadius = 15;
    _topLabel.userInteractionEnabled = YES;
    self.navigationItem.titleView = _topLabel;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTextfieldAction)];
    [_topLabel addGestureRecognizer:tap];
    
    _search = [[UIImageView alloc] initWithFrame:CGRectMake(5, 7.5, 15, 15)];
    _search.image = IMAGE(@"home_search_default");
    [_topLabel addSubview:_search];
    
    _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, -64, GWIDTH, GHEIGHT - 49) style:UITableViewStylePlain];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    [_tableview registerClass:[HomeBannerCell class] forCellReuseIdentifier:@"banner"];
    [_tableview registerClass:[HomeCategoryCell class] forCellReuseIdentifier:@"category"];
    [_tableview registerClass:[HomeActivityCell class] forCellReuseIdentifier:@"activity"];
    [_tableview registerClass:[HomeRecommendCell class] forCellReuseIdentifier:@"recommend"];
    [_tableview registerClass:[HomeListTableViewCell class] forCellReuseIdentifier:@"cell"];
    [_tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"xxxcell"];
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableview];
    
    _tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(HomdedoRefresh)];
    [self.tableview.mj_header beginRefreshing];
    
    UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, GWIDTH, 54)];
    iv.image = IMAGE(@"home_bottom");
    _tableview.tableFooterView = iv;
    _tableview.sectionFooterHeight = 54;
    
}

#pragma mark - 搜索框的响应方法
- (void)handleTextfieldAction
{
    SearchViewController *searchVC =  [SearchViewController new];
    searchVC.hidesBottomBarWhenPushed = YES;
    searchVC.navigationItem.hidesBackButton = YES;
    [self.navigationController pushViewController:searchVC animated:YES];
}

- (void)addCityBtn
{
    
//        if (cityName.length > 3) {
//            cityName = [NSString stringWithFormat:@"%@...",[cityName substringToIndex:3]];
//        }
    UIFont *cityFont = PFRFONT(12);
    _cityName = CITYNAME;
    if([GW_Tool isEmptyString:CITYNAME])
    {
        _cityName = @"哈尔滨市";
    }
    float cityWidth = [GW_Tool sizeOfLabelWithString:_cityName font:cityFont width:MAXFLOAT].width;
    UIImage *image = [IMAGE(@"home_arrow_down_default@2x") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    NSLog(@" 城市名 %@", CITYNAME);
    _cityBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    _cityBtn.frame = CGRectMake(0, 0, cityWidth+20, 44);
    _cityBtn.exclusiveTouch = YES;
    _cityBtn.titleLabel.font = cityFont;
    [_cityBtn setTitleColor:ColorWhite forState:UIControlStateNormal];
    [_cityBtn setTitle:_cityName forState:UIControlStateNormal];
    [_cityBtn setImage:[IMAGE(@"home_arrow_down_default@2x") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
//    [_cityBtn layoutIfNeeded];
    [_cityBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -image.size.width*2, 0, image.size.width)];
    [_cityBtn setImageEdgeInsets:UIEdgeInsetsMake(0, cityWidth+2, 0, 0)];
    
    [_cityBtn addTarget:self action:@selector(handleLocationAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_cityBtn];
}

#pragma mark - 定位相应方法
- (void)handleLocationAction
{
    HomeCitySelectViewController *homeVC = [HomeCitySelectViewController new];
    homeVC.hidesBottomBarWhenPushed = YES;
    @weakobj(self);
    homeVC.cityBlock = ^(NSString *cityName) {
        @strongobj(self);
        self.cityName = cityName;
        [self.cityBtn setTitle:self.cityName forState:UIControlStateNormal];
    };
    
    [self.navigationController pushViewController:homeVC animated:YES];
}

#pragma mark - UITableviewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4 + self.datasource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if(indexPath.row == 0)
        return 150 + 64;
    else if (indexPath.row == 1)
        return 117;
    else if (indexPath.row == 2)
        return 127;
    else if (indexPath.row == 3)
        return 70;
    else
        return 300;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0)
    {
        HomeBannerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"banner"];
        [cell configWithView:self.bannerArray];
        cell.actionArray = [self.actionArray copy];
        cell.viewcontroller = self;
        return cell;
    }else if (indexPath.row == 1)
    {
        HomeCategoryCell *cell = [tableView dequeueReusableCellWithIdentifier:@"category"];
        [cell configWith:_iconArray];
        cell.viewcontroller = self;
        return cell;
    }
    else if (indexPath.row == 2)
    {
        HomeActivityCell *cell = [tableView dequeueReusableCellWithIdentifier:@"activity"];
        [cell configWithImage:self.activeArray];
        [cell.firstBtn addTarget:self action:@selector(handleFirstBtn) forControlEvents:UIControlEventTouchUpInside];
        [cell.secondBtn addTarget:self action:@selector(handleSecondBtn) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }
    else if(indexPath.row == 3)//爆款推荐
    {
        HomeRecommendCell *cell = [tableView dequeueReusableCellWithIdentifier:@"recommend"];
        return cell;
    }else{
        HomeListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        [cell.goToPay addTarget:self action:@selector(handleGotoPay:) forControlEvents:UIControlEventTouchUpInside];
        cell.goToPay.tag = indexPath.row - 4 + 1000;
        HomeListModel *model = self.datasource[indexPath.row -  4];
        [cell configWithModel:model];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];//取消被选中的颜色
    if(indexPath.row == 2)
        return;
    if(indexPath.row == 3)
    {
        HomeRecommendDetailViewController *recommendVC = [HomeRecommendDetailViewController new];
        recommendVC.hidesBottomBarWhenPushed = YES;
        recommendVC.navigationItem.hidesBackButton = YES;
        [self.navigationController pushViewController:recommendVC animated:YES];
    }
    else
    {
        NSString *lat = [[NSUserDefaults standardUserDefaults] objectForKey:@"lat"];
        NSString *lon = [[NSUserDefaults standardUserDefaults] objectForKey:@"long"];
        HomeListModel *model = self.datasource[indexPath.row - 4];
        [GW_webView handleWebView:[NSString stringWithFormat:@"%@/product/productDel.html?gid=%@&lat=%@&lon=%@",GW_Domain, model.id, lat, lon] viewController:self titleName:@"首页" goodid:model.id];
    }
}

//附近活动响应按钮
- (void)handleFirstBtn
{
    [self handleTextfieldAction];
}
//限时体验响应按钮
- (void)handleSecondBtn
{
    self.tabBarController.selectedIndex = 2;
}


//立即购买按钮
- (void)handleGotoPay:(UIButton *)sender
{
    if(!IS_LOGIN)
    {
        MESSAGE_TOAST(@"请先登录");
        [[NSNotificationCenter defaultCenter] postNotificationName:kNotiShowLoginVC object:nil];
        return;
    }
    HomeListModel *model = self.datasource[sender.tag - 1000];
    //测试用的
    SubmitOrderViewController *payVC = [SubmitOrderViewController new];
    payVC.hidesBottomBarWhenPushed = YES;
    payVC.goodsName = model.goods_name;
    payVC.sale_price = model.sale_price;
    payVC.goodId = model.id;
    [self.navigationController pushViewController:payVC animated:YES];
}


- (void)setNavigationBarTranslucent
{
    [[UINavigationBar appearance] setTranslucent:YES];//设置导航条透明
    
    //设置导航栏背景图片为一个空的image，这样就透明了
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    
    //去掉透明后导航栏下边的黑边
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if(scrollView.contentOffset.y > 64)
    {
        [[UINavigationBar appearance] setTranslucent:NO];
        [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
        [self.navigationController.navigationBar setShadowImage:nil];
        
        self.navigationController.navigationBar.barTintColor = ColorFrom16(0xFFDB5A);
        _topLabel.textColor = ColorWhite;
        _topLabel.layer.backgroundColor = ColorFrom16(0xFFD231).CGColor;
        _search.image = IMAGE(@"home_search");
        [_cityBtn setImage:[IMAGE(@"home_arrow-down@2x") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:0];
        [_cityBtn setTitleColor:ColorFrom16(0x000000) forState:0];

    }
    else
    {
        self.navigationController.navigationBar.translucent = YES;//这样设置透明度
        [[UINavigationBar appearance] setTranslucent:YES];
        //设置导航栏背景图片为一个空的image，这样就透明了
//        [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];

        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:ColorClear] forBarMetrics:UIBarMetricsDefault];

        //去掉透明后导航栏下边的黑边
        [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
        _topLabel.layer.backgroundColor = ColorWhite.CGColor;
        _topLabel.textColor = ColorFrom16A(0x999999, 0.57);
//        [self setNavigationBarTranslucent];
        _search.image = IMAGE(@"home_search_default");
        [_cityBtn setImage:[IMAGE(@"home_arrow_down_default@2x") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:0];
        [_cityBtn setTitleColor:ColorWhite forState:0];
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    
    if(self.tableview.contentOffset.y > 64)
    {
        [[UINavigationBar appearance] setTranslucent:NO];
        [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
        [self.navigationController.navigationBar setShadowImage:nil];
        
        self.navigationController.navigationBar.barTintColor = ColorFrom16(0xFFDB5A);
        _topLabel.textColor = ColorWhite;
        _topLabel.layer.backgroundColor = ColorFrom16(0xFFD231).CGColor;
        _search.image = IMAGE(@"home_search");
        [_cityBtn setImage:[IMAGE(@"home_arrow-down@2x") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:0];
        [_cityBtn setTitleColor:ColorFrom16(0x000000) forState:0];
        
    }
    else
    {
        
        [self setNavigationBarTranslucent];
        self.navigationController.navigationBar.translucent = YES;//这样设置透明度
        [[UINavigationBar appearance] setTranslucent:YES];
        //设置导航栏背景图片为一个空的image，这样就透明了
        //        [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
        
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:ColorClear] forBarMetrics:UIBarMetricsDefault];
        
        //去掉透明后导航栏下边的黑边
        [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
        _topLabel.layer.backgroundColor = ColorWhite.CGColor;
        _topLabel.textColor = ColorFrom16A(0x999999, 0.57);
        //        [self setNavigationBarTranslucent];
        _search.image = IMAGE(@"home_search_default");
        [_cityBtn setImage:[IMAGE(@"home_arrow_down_default@2x") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:0];
        [_cityBtn setTitleColor:ColorWhite forState:0];
    }
    
    if([GW_Request sharedInstance].noNetwork)
    {
        self.load.hidden = NO;
    }
    [self.view endEditing:YES];
    NSString *appVersion = [[NSUserDefaults standardUserDefaults]objectForKey:kUDAppVersion];
    
    if (![appVersion isEqualToString:APP_VERSION]) {
        // 新手引导（在引导页注册点击广告的通知）
        UINavigationController *guideNav = [[UINavigationController alloc] initWithRootViewController:[GuideViewController new]];
        guideNav.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:guideNav animated:NO completion:nil];
    } else {
//         注册点击广告的通知
        [[NSNotificationCenter defaultCenter] removeObserver:self name:kNotiADClick object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleAdClickNoti:) name:kNotiADClick object:nil];
    }
    self.navigationController.navigationBar.backgroundColor = ColorClear;
    [super viewWillAppear:animated];
}

- (void)handleAdClickNoti:(NSNotification *)noti {
    
    // 点击广告的通知
    if (noti.object && [noti.object isKindOfClass:[NSString class]]) {
        
        NSString *webUrl = noti.object;
        WebViewController *webVC = [WebViewController new];
        webVC.urlStr = webUrl;
        webVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:webVC animated:YES];
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [self.view endEditing:YES];
    //设置导航栏不透明
    [[UINavigationBar appearance] setTranslucent:NO];
    self.navigationController.navigationBar.translucent = NO;//不透明
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
    self.navigationController.navigationBar.barTintColor = ColorWhite;
}

- (void)viewDidAppear:(BOOL)animated
{
    
}

- (void)viewDidDisappear:(BOOL)animated
{
    [self.alert removeFromSuperview];
}

- (void)alertAction
{
    _alert.hidden = NO;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
@end
